//
//  OnlineViewController.m
//  A段项目
//
//  Created by dllo on 16/5/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "OnlineViewController.h"
#import "MVViewController.h"
#import "RankingListViewController.h"
#import "RecomendViewController.h"
#import "SongerViewController.h"
#import "SongListViewController.h"
#import "VocalConcerViewController.h"
#import "MyCollectionViewCell.h"

@interface OnlineViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain)MVViewController *MVVC;
@property (nonatomic, retain)RankingListViewController *rankingListVC;
@property (nonatomic, retain)RecomendViewController *recomendVC;
@property (nonatomic, retain)SongerViewController *songerVC;
@property (nonatomic, retain)SongListViewController *songListVC;
@property (nonatomic, retain)VocalConcerViewController *vocalConcerVC;
@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)NSArray *array;
@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, assign)CGFloat positionOfScrollView;
@end

@implementation OnlineViewController

- (void)dealloc
{
    [_scrollView release];
    [_MVVC release];
    [_rankingListVC release];
    [_recomendVC release];
    [_songListVC release];
    [_songerVC release];
    [_vocalConcerVC release];
    [_collectionView release];
    [_array release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.array = @[@"推荐", @"排行榜", @"演唱会", @"歌单", @"MV", @"歌手"];
    [self creatView];
    [self addChildVC];
}

- (void)creatView {
//    self.automaticallyAdjustsScrollViewInsets = NO;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(WIDTH / 4, 30);
    layout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH  , 40) collectionViewLayout:layout];
    [layout release];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 40, WIDTH, HEIGHT - 40)];
    self.scrollView.contentSize = CGSizeMake(WIDTH * self.array.count, 0);
    self.scrollView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:self.scrollView];
    [_scrollView release];
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.tag = 10000000;
}

- (void)addChildVC {
    self.recomendVC = [[RecomendViewController alloc] init];
    self.rankingListVC = [[RankingListViewController alloc] init];
    self.vocalConcerVC = [[VocalConcerViewController alloc] init];
    self.songListVC = [[SongListViewController alloc] init];
    self.MVVC = [[MVViewController alloc] init];
    self.songerVC = [[SongerViewController alloc] init];
    [self addChildViewController:self.recomendVC];
    [self addChildViewController:self.rankingListVC];
    [self addChildViewController:self.vocalConcerVC];
    [self addChildViewController:self.songListVC];
    [self addChildViewController:self.MVVC];
    [self addChildViewController:self.songerVC];
    [_recomendVC release];
    [_rankingListVC release];
    [_vocalConcerVC release];
    [_songerVC release];
    [_songListVC release];
    [_MVVC release];
    [self.scrollView addSubview:self.recomendVC.view];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.positionOfScrollView = scrollView.contentOffset.x / WIDTH;
    NSLog(@"%f",self.positionOfScrollView);
    NSLog(@"%f",scrollView.contentOffset.x);
    if (scrollView == self.scrollView) {
        for (int i = 0; i < 6; i++) {
            if (scrollView.contentOffset.x == WIDTH * i) {
                if (![self.childViewControllers[i].view isDescendantOfView:scrollView]) {
                    [scrollView addSubview:self.childViewControllers[i].view];
                    self.childViewControllers[i].view.frame = CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT);
                }
            }
        }
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:scrollView.contentOffset.x / WIDTH inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        [self collectionView:self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:scrollView.contentOffset.x / WIDTH inSection:0]];
        
    }
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    // 重用来解决
    
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
//    NSLog(@"%f",self.positionOfScrollView);
    cell.naviLabel.text = self.array[indexPath.row];
    if (indexPath.row == self.positionOfScrollView) {
        NSLog(@"33333333");
        cell.naviLabel.backgroundColor = [UIColor redColor];
    }
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
